<template>
    <div>
        <!-- 商品列表区域 -->
        <div class="goods-list">
            <router-link class="goods-item" v-for="item in goodslist" :key="item.id" :to="'/home/goodsinfo/'+item.id" tag="div">
                <img :src="item.img_url">
                <h3 class="title">{{item.title}}</h3>
                <div class="info">
                    <p class="price">
                        <span class="new">￥{{item.market_price}}</span>
                        <span class="old">￥{{item.sell_price}}</span>
                    </p>
                    <p class="sell">
                        <span>热卖中</span>
                        <span>剩余{{item.stock_quantity}}件</span>
                    </p>    
                </div>
            </router-link>

            <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
        </div>
    </div>
</template>

<script>
import {Toast} from 'mint-ui'
export default {
    data(){
        return{
            page:1,
            goodslist:[],//商品列表
            isloaded:false//添加节流阀
        }
    },
    methods:{
        async getGoodsListByPage(){
            const {data} = await this.$http.get('/api/getgoods?pageindex='+this.page)
            if(data.status===0) this.goodslist=this.goodslist.concat(data.message)//concat防止第二页数据覆盖第一页
            if(data.message.length<10) this.isloaded=true//如果获取的message长度为0则数据加载完毕
        },
        getMore(){
            if(this.isloaded) Toast('没有更多了')
            this.page++
            this.getGoodsListByPage()
        }
    },
    created(){
        this.getGoodsListByPage()
    }
}
</script>
<style lang="scss" scoped>
    .goods-list{
        padding: 2%;
        display: flex;
        flex-wrap: wrap;//长度超出自动换行
        justify-content: space-between;
       .goods-item{
           //防止title字数影响布局
           display: flex;
           flex-direction: column;
           justify-content: space-between;
           border: 1px solid #ccc;
           width: 49%;
           margin-bottom: 2%;
           padding: 1%;
           box-shadow: 0 0 7px #ccc;
           img{
               width: 100%;
               min-height: 170px;
           }
           .title{
            //防止title字数影响布局
            //    min-height: 42px;
               font-size: 14px;
           }
           .info{
               overflow: hidden;
               background-color: #eee;
               .price{
                   margin-bottom: 2%;
                   .new{
                       color: red;
                       font-size: 16px;
                       font-weight: bold;
                       margin-right: 5%;
                       margin-left: 3%;
                   }
                   .old{
                       text-decoration: line-through;
                       font-size: 12px;
                   }
               }
               .sell{
                    margin-bottom: 0;
                    padding: 3%;
                    font-size: 13px;
                    display: flex;
                    justify-content: space-between;
                }
           }
       }
    }
</style>